package com.eight.cloud.admin.controller;

import com.eight.cloud.admin.service.ISystemService;
import com.eight.cloud.core.global.result.Result;
import com.eight.cloud.core.global.result.ResultCode;
import com.eight.cloud.openfeign.admin.vo.permission.PermissionTree;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 系统控制层
 *
 * @ClassName: SystemController
 * @Author: TXC
 * @Date: 2024-10-18 17:44
 **/
@RestController
@RequestMapping("/system")
public class SystemController {
    @Resource
    private ISystemService systemService;

    /**
     * 初始化系统
     * @return 结果
     */
    @GetMapping("/init")
    public Result<String> initSystem() {
        boolean res = systemService.initSystem();
        if (!res) {
            return Result.fail(ResultCode.ERROR.getCode(), "系统初始化失败");
        }
        return Result.ok();
    }

    /**
     * 刷新权限
     * @return 结果
     */
    @PostMapping("/flushPermission")
    public Result<String> flushPermission(@RequestBody PermissionTree permissionTree) {
        boolean res = systemService.initPermission(permissionTree);
        if (!res) {
            return Result.fail(ResultCode.ERROR.getCode(), "系统权限初始化失败");
        }
        systemService.flushAdminPermission();
        return Result.ok();
    }
}
